import pygame
import random

def gen_point():
    return [random.randint(20,480), 20]


def start():
    pygame.init()
    screen = pygame.display.set_mode((500, 700))
    pygame.display.set_caption('接球游戏')
    clock = pygame.time.Clock()
    is_loop =True
    m_pos = []
    m_pos.append(gen_point())
    rect = [200,600, 100, 30]
    index = 0
    score = 0
    life = 2
    font = pygame.font.SysFont('宋体', 30)

    while is_loop:
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                is_loop=False
            if e.type == pygame.KEYDOWN:
                if e.key == pygame.K_LEFT:
                    rect[0] -= 10
                    if rect[0]<0:
                        rect[0] = 10
                elif e.key == pygame.K_RIGHT:
                    rect[0] += 10
                    if rect[0] > 500-rect[2]:
                        rect[0]= 500-rect[2]-10

        screen.fill((0,0,0))
        n = 0
        while n < len(m_pos):
            pos = m_pos[n]
            pygame.draw.circle(screen, (255,0,0),pos, 20)
            if pos[1]>580 and pos[1]<610:
                if pos[0]+20>rect[0] and pos[0]-20< rect[0]+rect[2]:
                    del m_pos[n]
                    score+=1
                    continue
            elif pos[1]> 700:
                del m_pos[n]
                life-=1
                continue
            n+=1
            pos[1]+=2
        if index == 60:
            index = 0
            m_pos.append(gen_point())
        else:
            index+=1
        s_image = font.render(f"Score:{score}", True, [255,0,0])
        screen.blit(s_image, (10,10))
        s_life = font.render(f"life:{life}", True, [255, 0, 0])
        screen.blit(s_life, (10, 30))
        pygame.draw.rect(screen, (0, 255,0), rect)
        clock.tick(60)
        pygame.display.update()

    pygame.quit()

start()